package com.markyao.service.impl;

import com.markyao.dao.mapper.CategoryMapper;
import com.markyao.dao.pojo.ProductCategory;
import com.markyao.service.CategoryService;
import com.markyao.vo.CategoryVo;
import com.markyao.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Result findList() {
        List<ProductCategory> categoryList=categoryMapper.findAll();
        List<CategoryVo>categoryVoList=copyList(categoryList);
        return new Result(true,"查询成功",111,categoryVoList);
    }

    @Override
    public String getNameByCid(Integer cid) {
        return categoryMapper.getNameByCid(cid);
    }

    private List<CategoryVo> copyList(List<ProductCategory> categoryList) {
        List<CategoryVo>categoryVoList=new ArrayList<>();
        for (ProductCategory productCategory : categoryList) {
            CategoryVo categoryVo=new CategoryVo();
            BeanUtils.copyProperties(productCategory,categoryVo);
            categoryVo.setParentId(String.valueOf(productCategory.getParentId()));
            categoryVo.setId(String.valueOf(productCategory.getId()));
            categoryVoList.add(categoryVo);
        }
        return categoryVoList;
    }
}
